<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: HCZ <huangchengze@etrol.com> 
// +----------------------------------------------------------------------

namespace app\audit\controller\api;
use app\common\controller\Api;
use think\response\Json;
use app\common\model\Wfprocess;

/**
 * 审核流程(我的审核)
 * @package app\Craft
 * @author HCZ<huangchengze@etrol.com>
 */
class Check extends Api

{

    /**
   *  危化工艺列表
   * @param string  string equipeId 企业id,如果有只显示企业自己的信息.
   * @return Json
   */

	public function wfProcessList($chargePerson = null,$wfType = null){
		//分页参数
		$hasWhere = [];
		if($chargePerson){
			$hasWhere['chargePerson'] = $chargePerson;
		}
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			// 审核进程列表
			$processes = model('Wfprocess')->hasWhere('wfstep',$hasWhere)->with('wfitem,wfstep')->limit($startLimit.','.$limit)->select();
		}else{
			// 审核进程列表
			$processes = model('Wfprocess')->hasWhere('wfstep',$hasWhere)->with('wfitem,wfstep')->select();
		}
		if($wfType){
			$info = [];
		   	foreach($processes as $process){
		   		if($process->wfitem->wfTypeId == $wfType){
		   			$info[] = $process;
		   		}
		   	}
		}else{
			$info = $processes;
		}
		$info = collection($info)->toArray();
	 	//总数
	   	 $count = model('Wfprocess')->count('id'); 
		
    		if ($count == 0) {
         			return json(['code'=>1,'msg'=>'','count'=>$count,'data'=>$info]);
       		} 
       	
       		if ($count > 0){
       			return json(['code'=>1,'msg'=>'','count'=>$count,'data'=>$info]);
       		}
         return json(['code'=>0,'msg'=>'数据异常']);
   	 }
   	 
   	 /**
   	  * 审核项目信息
   	  * @param string $processId
   	  */
   	 public function getCheckItemInfo($processId){
   	 	if($processId){
   	 		$process  = model('Wfprocess')->with('wfitem,wfstep')->where('id',$processId)->find();
   	 		//危险源
	   	 	if(in_array($process->wfitem->wfTypeId,[3,6,12])){
		  			$business = model($process->wfitem->busiModel)->with('enterprise,staff,sensors')->where('id',$process->wfitem->busiId)->find();
		  			$info['item'] = $business->toArray();
		  			$info['item']['templet'] = 'dangersource';
		  	}
		  	//物资装备
		  	if(in_array($process->wfitem->wfTypeId,[10])){
		  			$business = model($process->wfitem->busiModel)->with('enterprise,store,store.staff')->where('id',$process->wfitem->busiId)->find();
		  			$info['item'] = $business->toArray();
		  			$info['item']['templet'] = 'equipe';
		  	}
		  	//企业信息
		  	if(in_array($process->wfitem->wfTypeId,[11])){
		  			$business = model($process->wfitem->busiModel)->where('id',$process->wfitem->busiId)->find();
		  			$info['item'] = $business->append(['position','area','production','type','unit'])->toArray();
		  			$info['item']['templet'] = 'enterprise';
		  	}
		  	//救援单位
		  	if(in_array($process->wfitem->wfTypeId,[7,8,9])){
		  			$business = model($process->wfitem->busiModel)->where('id',$process->wfitem->busiId)->find();
		  			$info['item'] = $business->toArray();
		  			$info['item']['templet'] = 'rescueunit';
		  	}
		  	//预案
		  	if(in_array($process->wfitem->wfTypeId,[1,4,5])){
		  		if($process->wfitem->wfTypeId == 5){
		  			$with = 'scripts,enterprise,scripts.staff';
		  		}else{
		  			$with = 'scripts,unit';
		  		}
		  		$business = model($process->wfitem->busiModel)->with($with)->where('id',$process->wfitem->busiId)->find();
		  		$info['item'] = $business->toArray();
		  		$info['item']['templet'] = 'plan';
		  	}
		  	//信息发布
		  	if($process->wfitem->wfTypeId == 13){
		  		$business = model($process->wfitem->busiModel)->with('user')->where('id',$process->wfitem->busiId)->find();
		  		$info['item'] = $business->toArray();
		  		$info['item']['templet'] = 'news';
		  	}
	   	 	$info['process'] = $process->toArray();
	   	 	return json(['code'=>1,'msg'=>'','data'=>$info]);
   	 	}
   	 	return json(['code'=>0,'msg'=>'数据异常','data'=>'']);
   	 }
   	 
   	 /**
   	  * 审核
   	  * @param array $data
   	  */
   	 public function check($data = []){
   	 	if(empty($data)){
   	 		$data = input('post.');
   	 	}
   	 	//审核时间
   	 	$data['endTime'] = time();
   	 	//更新流程状态
   	 	model('Wfprocess')->change($data);
   	 	$wfprocess = model('Wfprocess')->find($data['id']);
   	 	if($data['approvalFlag'] == 1){
   	 		//通过审核
   	 		if($wfprocess->wfitem->workflow->stepTotal == $wfprocess->wfitem->wfStep){
   	 			//最后一步
   	 			if($wfprocess->wfitem->wfTypeId == 11){
   	 				$status = 'infoCheckStatus';
   	 			}else{
   	 				$status = 'checkStatus';
   	 			}
   	 			//审核事项完成
   	 			$wfprocess->wfitem->status = 2; //审核成功
   	 			$wfprocess->wfitem->save();
   	 			//审核信息完成
   	 			model($wfprocess->wfitem->busiModel)->save([$status=>3],['id'=>$wfprocess->wfitem->busiId]); //已审核
   	 			return json(['code'=>1,'msg'=>'事项审核完毕']);
   	 		}else if($wfprocess->wfitem->workflow->stepTotal > $wfprocess->wfitem->wfStep){
   	 			//不是最后一步
   	 			//下一步
   	 			$nextStep = $wfprocess->wfitem->workflow->steps[$wfprocess->wfitem->wfStep];
   	 			$nextProcess['wfItemInfoId'] = $wfprocess->wfitem->id;
   	 			$nextProcess['wfStepInfoId'] = $nextStep->id;
   	 			$nextProcess['approvalRetIndex'] = $wfprocess->id;
   	 			$nextProcess['operationId'] = $nextStep->chargePerson;
   				$process = new Wfprocess();
   	 			$process->save($nextProcess);
   	 			//事项进入下一步
   	 			$wfprocess->wfitem->wfStep = $wfprocess->wfitem->wfStep+1;
   	 			$wfprocess->wfitem->save();
   	 			return json(['code'=>1,'msg'=>'进入下一流程']);
   	 		}
   	 	}
   	 	if($data['approvalFlag'] == 2){
   	 		//退回,补充资料后可再次提交
   	 		//审核事项完成
   	 		$wfprocess->wfitem->status = 3; //审核失败
   	 		$wfprocess->wfitem->save();
   	 		//审核信息打回
   	 		if($wfprocess->wfitem->wfTypeId == 11){
   	 			$status = 'infoCheckStatus';
   	 		}else{
   	 			$status = 'checkStatus';
   	 		}
   	 		model($wfprocess->wfitem->busiModel)->save([$status=>1],['id'=>$wfprocess->wfitem->busiId]);//未审核
   	 		return json(['code'=>1,'msg'=>'信息退回']);
   	 	}
   	 	if($data['approvalFlag'] == 3){
   	 		//否决,审核不通过
   	 		$wfprocess->wfitem->status = 3; //审核失败
   	 		$wfprocess->wfitem->save();
   	 		//审核信息不成功
   	 		if($wfprocess->wfitem->wfTypeId == 11){
   	 			$status = 'infoCheckStatus';
   	 		}else{
   	 			$status = 'checkStatus';
   	 		}
   	 		model($wfprocess->wfitem->busiModel)->save([$status=>4],['id'=>$wfprocess->wfitem->busiId]);//审核不通过
   	 		return json(['code'=>1,'msg'=>'审核否决']);
   	 	}
   	 	return json(['code'=>0,'msg'=>'审核失败']);
   	 }
} 	  
